package com.fancy.service.imp;

import com.alibaba.fastjson.JSON;
import com.fancy.dao.RankMusicDao;
import com.fancy.pojo.category.Music;
import com.fancy.service.RankVisitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.DefaultTypedTuple;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class RankVisitServiceImp implements RankVisitService {
    @Autowired
    RankMusicDao rankMusicDao;
    @Autowired
    RedisTemplate redisTemplate;

    public static final String SCORE_RANK = "visit";

//    public void Add() {
//        Set<ZSetOperations.TypedTuple<String>> tuples = new HashSet<>();
//        long start = System.currentTimeMillis();
//        for (Music music:allMusic) {
//            DefaultTypedTuple<String> tuple = new DefaultTypedTuple<>(music, music.getVisitQuantity());
//            tuples.add(tuple);
//        }
//        Long num = redisTemplate.opsForZSet().add(SCORE_RANK, tuples);
//
//    }

    @Override
    public String findAllMusic() {

        Set<ZSetOperations.TypedTuple<Music>> tuples = new HashSet<>();
        long start = System.currentTimeMillis();
        List<Music> allMusic = rankMusicDao.findAllMusic();
        for (Music music:allMusic) {
            Integer visitQuantity = music.getVisitQuantity();
            String s = Integer.toString(visitQuantity);
            double v = Double.parseDouble(s);
            DefaultTypedTuple<Music> tuple = new DefaultTypedTuple<Music>(music,v);
            tuples.add(tuple);
        }
        Long num = redisTemplate.opsForZSet().add(SCORE_RANK, tuples);

        Set<String> range = redisTemplate.opsForZSet().reverseRange(SCORE_RANK, 0, 10);
//        System.out.println("获取到的排行列表:" + JSON.toJSONString(range));
        Set<ZSetOperations.TypedTuple<Music>> rangeWithScores = redisTemplate.opsForZSet().reverseRangeWithScores(SCORE_RANK, 0, 10);
        System.out.println("获取到的排行和分数列表:" + JSON.toJSONString(rangeWithScores));
//        System.out.println(rangeWithScores);

//        //浏览量排行
//        for (Music music:allMusic) {
//            redisTemplate.opsForZSet().add("visit", music, music.getVisitQuantity());
//        }
//        Set visit = redisTemplate.opsForZSet().reverseRange("visit", 0, 10);
//        RankResult rankResult= new RankResult();
//        rankResult.setSet(rangeWithScores);


//        return rankResult;
        return JSON.toJSONString(rangeWithScores);
    }

    @Override
    public String ridesFindAllMusic() {
//        Set<ZSetOperations.TypedTuple<Object>> visit = redisTemplate.opsForZSet().reverseRangeWithScores("visit", 0, -1);
//        Iterator<ZSetOperations.TypedTuple<Object>> iterator = visit.iterator();
//        List<MusicRankResult> list = new ArrayList<>();
//        MusicRankResult musicRankResult=null;
//        while(iterator.hasNext()){
//            ZSetOperations.TypedTuple<Object> next = iterator.next();
//            musicRankResult.setScore(next.getScore());
//        }


//        Set visit = redisTemplate.opsForZSet().range("visit", 0, 10);
//        RankResult rankResult= new RankResult();
//        rankResult.setSet(visit);
//        return rankResult;
        Set<String> range = redisTemplate.opsForZSet().reverseRange(SCORE_RANK, 0, 10);
//        System.out.println("获取到的排行列表:" + JSON.toJSONString(range));
        Set<ZSetOperations.TypedTuple<Music>> rangeWithScores = redisTemplate.opsForZSet().reverseRangeWithScores(SCORE_RANK, 0, 10);
        System.out.println("获取到的排行和分数列表:" + JSON.toJSONString(rangeWithScores));
        return JSON.toJSONString(rangeWithScores);

//        Set<ZSetOperations.TypedTuple<Object>> foodName = redisTempalte.opsForZSet().reverseRangeWithScores("foodName", 0, -1);
//        Iterator<ZSetOperations.TypedTuple<Object>> iterator = foodName.iterator();
//        List<HPFood> list = new ArrayList<>();
//        HPFood hpFood=null;
//        while(iterator.hasNext()){
//            ZSetOperations.TypedTuple<Object> next = iterator.next();
//            hpFood=new HPFood();
//            hpFood.setFoodName((String)next.getValue());
//            hpFood.setScore((Double)next.getScore());
//            hpFood.setFoodImg("http://qge2iwl09.hn-bkt.clouddn.com/11.png");
//            list.add(hpFood);
//        }
//        System.out.println(list.toString());
//        BaseResp baseResp = new BaseResp();
//        baseResp.setList(list);
//        return baseResp;
    }

    @Override
    public void addvisit(Music music) {

//        redisTemplate.opsForZSet().remove("visit", music);
//        System.out.println(music);
//        Integer visitQuantity = music.getVisitQuantity();
//        visitQuantity = visitQuantity + 1;
//        music.setVisitQuantity(visitQuantity);
//        redisTemplate.opsForZSet().add("visit", music, 0.1-music.getVisitQuantity());
        Double score = redisTemplate.opsForZSet().incrementScore(SCORE_RANK, music, 1);
        System.out.println("李四分数+1000后：" + score);
    }

    @Override
    public List<Music> findAllMusic1() {
        List<Music> allMusic = rankMusicDao.findAllMusic();
        return allMusic;
    }
}
